﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using X.PagedList;
using XbsWebAPI.EntityFrameworkCore;
using XbsWebAPI.EntityFrameworkCore.Entities;
using XbsWebAPI.EntityFrameworkCore.Entities.Enums;

namespace XbsWebAPI.Core.Repositories
{
    /// <summary>
    /// 营位领域模型 实现
    /// </summary>
    public class HouseTypeRepository : IHouseTypeRepository
    {
        private readonly DataContext _dbContext;

        public HouseTypeRepository(DataContext dataContext)
        {
            _dbContext = dataContext;
        }

        public HouseType Create(HouseType entity)
        {
            entity.Id = Guid.NewGuid();
            entity.CreationDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            _dbContext.HouseTypes.Add(entity);
            //_dbContext.HouseTypeSupportingFacilities.Add(entity.HouseTypeSupportingFacilities);
            _dbContext.SaveChanges();

            return entity;
        }

        public Task<HouseType> CreateAsync(HouseType entity)
        {
            return Task.FromResult(Create(entity));
        }

        public HouseType Delete(Guid id)
        {
            var model = _dbContext.HouseTypes.FirstOrDefault(x => x.Id == id);
            if (model == null)
                return null;
            _dbContext.Remove(model);
            _dbContext.SaveChanges();

            return model;
        }

        public Task<HouseType> DeleteAsync(Guid id)
        {
            return Task.FromResult(Delete(id));
        }

        public HouseType QueryById(Guid id)
        {
            return _dbContext.HouseTypes.FirstOrDefault(x => x.Id == id);
        }

        public IQueryable<HouseType> Query(Expression<Func<HouseType, bool>> filter)
        {
            var query = _dbContext.HouseTypes.Where(filter);

            return query;
        }

        public IQueryable<HouseType> AsQueryable()
        {
            var query = _dbContext.HouseTypes.AsQueryable();

            return query;
        }

        public HouseType Update(HouseType entity)
        {
            var model = _dbContext.HouseTypes.FirstOrDefault(x => x.Id == entity.Id);
            if (model == null)
                return null;

            model.UpdateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            _dbContext.HouseTypes.Update(model);
            _dbContext.SaveChanges();

            return entity;
        }

        public Task<HouseType> UpdateAsync(HouseType entity)
        {
            return Task.FromResult(Update(entity));
        }

        public void CreateInCludeImageAsync(HouseType entity, List<string> files)
        {
            entity.Id = Guid.NewGuid();
            entity.CreationDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            entity.HouseTypeStatus = HouseTypeStatus.HasBeenOn;
            _dbContext.HouseTypes.Add(entity);
            //_dbContext.HouseTypeSupportingFacilities.Add(entity.HouseTypeSupportingFacilities);
            if(files != null && files.Count > 0)
            {
                foreach (var filePath in files)
                {
                    var image = new HouseTypePicture
                    {
                        Id = Guid.NewGuid(),
                        CreationDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                        Name = "",
                        HouseTypeId = entity.Id,
                        PicUrl = filePath
                    };
                    _dbContext.HouseTypePictures.Add(image);
                }
            }
            
            
            _dbContext.SaveChanges();


        }

        public void UpdateInCludeImageAsync(HouseType entity, List<string> files)
        {
            var original = _dbContext.HouseTypes.AsNoTracking().FirstOrDefault(x => x.Id == entity.Id);
            if(original == null)
            {
                throw new Exception($"更新的Id={entity.Id}用户不存在");
            }
            //删除相关的配套设置中间表
            var typehouseSuporttings = _dbContext.HouseTypeSupportingFacilities.AsNoTracking().Where(a => a.HouseTypeId == entity.Id);
            if (typehouseSuporttings != null && typehouseSuporttings.Any())
            {
                _dbContext.HouseTypeSupportingFacilities.RemoveRange(typehouseSuporttings);
            }

            entity.CreationDate = original.CreationDate;
            entity.UpdateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            _dbContext.HouseTypes.Update(entity);

            //处理图片
            var houseTypeImages = _dbContext.HouseTypePictures.AsNoTracking().Where(a => a.HouseTypeId == entity.Id);
            _dbContext.HouseTypePictures.RemoveRange(houseTypeImages);

            if (files != null && files.Count > 0)
            {
                foreach (var filePath in files)
                {
                    var image = new HouseTypePicture
                    {
                        Id = Guid.NewGuid(),
                        CreationDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                        Name = "",
                        HouseTypeId = entity.Id,
                        PicUrl = filePath
                    };
                    _dbContext.HouseTypePictures.Add(image);
                }
            }


            _dbContext.SaveChanges();
        }
    }
}
